package com.zlzxm.shoesmarket.view.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.activity.manage.StartBundleKey;
import com.zlzxm.shoesmarket.view.adapter.AdGoodsInfo;
import com.zlzxm.shoesmarket.view.adapter.entity.EntityGoodsInfo;
import com.zlzxm.shoesmarket.view.markview.MarkViewPrice;
import com.zlzxm.shoesmarket.view.widget.simpleead.OnLeftclicklistener;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMBillboardDetail;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityChartValue;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityInOut;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zlz
 * on  4/10/21
 *
 * <br/>榜单详情
 */
public class ACBillboardDetail extends BaseActivity implements OnChartValueSelectedListener, View.OnClickListener {


    public static void start(Activity activity, ImageView sharedElement,
                             String urlFace){

        Intent intent = new Intent(activity,ACBillboardDetail.class);

        intent.putExtra(StartBundleKey.GOODS_PIC,urlFace);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            if(sharedElement != null) {

                activity.startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(activity, sharedElement,
                        activity.getString(R.string.transition_goods_pic)).toBundle());
            }else {

                activity.startActivity(intent);

            }

        }else {

            activity.startActivity(intent);

        }

    }

    private VMBillboardDetail vmBillboardDetail;

    private ImageView ivFace;

    private final AdGoodsInfo adGoodsInfo = new AdGoodsInfo(null);

    private RecyclerView rvInfo;

    private LineChart lcprice, lcInOut;

    private TextView tvPriceToDay,tvPriceSevenDay,tvPriceMonth,tvInOutSevenDay,tvInOutMonth;

    //<editor-fold defaultState="collapsed" desc="liveData">

    private final Observer<List<EntityGoodsInfo>> observerGoodsInfo  = adGoodsInfo :: setList;

    private final Observer<List<EntityChartValue>> observerPriceData  = this::initPriceChartData;

    private final Observer<EntityInOut> observerInOutData  = this::initInOutChartData;

    // </editor-fold>


    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.ac_billboard_detail);
        SimpleHead simpleHead =  ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("榜单详情");
        simpleHead.setOnItemclicklistener(new OnLeftclicklistener() {
            @Override
            public void onLeftItemClick() {
                finish();
            }
        });

        ivFace = ZViewHelp.findById(this,R.id.ivFace);

        rvInfo = ZViewHelp.findById(this,R.id.rvInfo);


        rvInfo.setLayoutManager(new LinearLayoutManager(this));

        rvInfo.setAdapter(adGoodsInfo);



        tvPriceToDay = ZViewHelp.setOnClickListener(this,R.id.tvPriceToDay,this);
        tvPriceSevenDay = ZViewHelp.setOnClickListener(this,R.id.tvPriceSevenDay,this);
        tvPriceMonth = ZViewHelp.setOnClickListener(this,R.id.tvPriceMonth,this);
        tvInOutSevenDay = ZViewHelp.setOnClickListener(this,R.id.tvInOutSevenDay,this);
        tvInOutMonth = ZViewHelp.setOnClickListener(this,R.id.tvInOutMonth,this);

        lcprice = ZViewHelp.findById(this,R.id.lcPrice);

        lcInOut = ZViewHelp.findById(this,R.id.lcInoOut);

        initPriceLineChart();

        initInOutLineChart();

    }


    @Override
    protected void initViewModule() {
        super.initViewModule();

        vmBillboardDetail = new ViewModelProvider(this).get(VMBillboardDetail.class);

        vmBillboardDetail.getLiveGoodInfo().observe(this,observerGoodsInfo);

        vmBillboardDetail.getLivePriceInfo().observe(this,observerPriceData);

        vmBillboardDetail.getLiveInOutInfo().observe(this,observerInOutData);

    }

    @Override
    protected void initData() {
        Log.e("ZLZ","initData");

        String url = getIntent().getStringExtra(StartBundleKey.GOODS_PIC);

        if(!TextUtils.isEmpty(url)) {
            Glide.with(this)
                    .load(url)
                    .into(ivFace);
        }

        priceTagSelected(VMBillboardDetail.PRICE_TODAY);

        inOutTagSelected(VMBillboardDetail.INOUT_SEVEN_DAY);

    }




    @Override
    public void onValueSelected(Entry e, Highlight h) {

    }

    @Override
    public void onNothingSelected() {

    }

    @Override
    public void onClick(View v) {

        int id = v.getId();

        if(id == R.id.tvPriceToDay){

            priceTagSelected(VMBillboardDetail.PRICE_TODAY);

        }else if(id == R.id.tvPriceSevenDay){

            priceTagSelected(VMBillboardDetail.PRICE_SEVEN_DAY);

        }else if(id == R.id.tvPriceMonth){

            priceTagSelected(VMBillboardDetail.PRICE_MONTH);

        }else if(id == R.id.tvInOutSevenDay){

            inOutTagSelected(VMBillboardDetail.INOUT_SEVEN_DAY);

        }else if(id == R.id.tvInOutMonth){

            inOutTagSelected(VMBillboardDetail.INOUT_MONTH);

        }



    }





    //<editor-fold defaultState="collapsed" desc="私有方法">


    private void priceTagSelected(int by){

        tvPriceToDay.setSelected(false);
        tvPriceSevenDay.setSelected(false);
        tvPriceMonth.setSelected(false);

        switch (by){

            case VMBillboardDetail.PRICE_TODAY:
                tvPriceToDay.setSelected(true);
                break;
            case VMBillboardDetail.PRICE_SEVEN_DAY:
                tvPriceSevenDay.setSelected(true);
                break;

            case VMBillboardDetail.PRICE_MONTH:
                tvPriceMonth.setSelected(true);
                break;

        }

        vmBillboardDetail.getPriceData(by);
    }



    private void inOutTagSelected(int by){

        tvInOutSevenDay.setSelected(false);
        tvInOutMonth.setSelected(false);

        switch (by){

            case VMBillboardDetail.INOUT_SEVEN_DAY:
                tvInOutSevenDay.setSelected(true);
                break;
            case VMBillboardDetail.INOUT_MONTH:
                tvInOutMonth.setSelected(true);
                break;


        }

        vmBillboardDetail.getInOutData(by);
    }



    private void initPriceLineChart(){

        lcprice.setOnChartValueSelectedListener(this);

        lcprice.setDragEnabled(false);
        //设置推动
        lcprice.setScaleEnabled(false);

        lcprice.setMarker(new MarkViewPrice(this));

        YAxis leftAxis = lcprice.getAxisLeft();
        YAxis rightAxis = lcprice.getAxisRight();


        leftAxis.setTextSize(12);
        leftAxis.setTextColor(ContextCompat.getColor(this,R.color.color403970));

        leftAxis.setAxisMinimum(0);
        rightAxis.setAxisMinimum(0);

        leftAxis.setDrawLabels(true);
        rightAxis.setDrawLabels(false);

        leftAxis.setDrawAxisLine(false);
        rightAxis.setDrawAxisLine(false);

        leftAxis.setAxisMaximum(100);
        rightAxis.setAxisMaximum(100);

        leftAxis.setLabelCount(6,true);
        rightAxis.setLabelCount(6,true);


        leftAxis.setGridColor(ContextCompat.getColor(this,
                R.color.colorC3CAFA));
        rightAxis.setGridColor(ContextCompat.getColor(this,
                R.color.colorC3CAFA));

        lcprice.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

        lcprice.getLegend().setEnabled(false);

        Description description = new Description();
        description.setText("");
        lcprice.setDescription(description);

        lcprice.getXAxis().setDrawGridLines(false);

        lcprice.setExtraBottomOffset(10);


        LineDataSet lineDataSet = new LineDataSet(new ArrayList<>(), "");

        lineDataSet.setDrawHorizontalHighlightIndicator(false);
        lineDataSet.setDrawVerticalHighlightIndicator(false);

        lineDataSet.setColor(ContextCompat
                .getColor(this,R.color.color5A73FF));

        lineDataSet.setLineWidth(2f);

        lineDataSet.setCircleRadius(5);

        lineDataSet.setCircleHoleRadius(4);

        lineDataSet.setCircleColor(ContextCompat
                .getColor(this,R.color.color5A73FF));

        lineDataSet.setDrawValues(false);

        lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);

        LineData lineData = new LineData(lineDataSet);


        lcprice.setData(lineData);


    }

    private void initInOutLineChart() {


        lcInOut.setOnChartValueSelectedListener(this);

        lcInOut.setDragEnabled(false);
        //设置推动
        lcInOut.setScaleEnabled(false);

        lcInOut.setMarker(new MarkViewPrice(this));

        YAxis leftAxis = lcInOut.getAxisLeft();
        YAxis rightAxis = lcInOut.getAxisRight();


        leftAxis.setTextColor(ContextCompat.getColor(this,R.color.colorA7A7A7));
        leftAxis.setTextSize(12);

        leftAxis.setAxisMinimum(0);
        rightAxis.setAxisMinimum(0);

        leftAxis.setDrawLabels(true);
        rightAxis.setDrawLabels(false);

        leftAxis.setDrawAxisLine(false);
        rightAxis.setDrawAxisLine(false);

        leftAxis.setAxisMaximum(100);
        rightAxis.setAxisMaximum(100);

        leftAxis.setLabelCount(6,true);
        rightAxis.setLabelCount(6,true);


        leftAxis.setGridColor(ContextCompat.getColor(this,
                R.color.colorA8A8A8));
        rightAxis.setGridColor(ContextCompat.getColor(this,
                R.color.colorA8A8A8));

        lcInOut.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

        lcInOut.getLegend().setEnabled(true);

        Description description = new Description();
        description.setText("");
        lcInOut.setDescription(description);

        lcInOut.getXAxis().setDrawGridLines(false);

        Legend legend =  lcInOut.getLegend();

        legend.setForm(Legend.LegendForm.CIRCLE);

        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setFormSize(10);
        legend.setXEntrySpace(20);
        legend.setTextSize(13);
        legend.setTextColor(ContextCompat.getColor(this,R.color.color333333));
        lcInOut.setExtraBottomOffset(20);




        LineDataSet lineDataSetOut = new LineDataSet(new ArrayList<>(), "出库值");
        LineDataSet lineDataSetIn = new LineDataSet(new ArrayList<>(), "入库值");

        lineDataSetOut.setDrawHorizontalHighlightIndicator(false);
        lineDataSetOut.setDrawVerticalHighlightIndicator(false);

        lineDataSetOut.setColor(ContextCompat
                .getColor(this,R.color.colorE44444));

        lineDataSetOut.setLineWidth(2f);

        lineDataSetOut.setDrawCircles(false);


        lineDataSetOut.setDrawValues(false);

        lineDataSetOut.setMode(LineDataSet.Mode.LINEAR);

        lineDataSetIn.setDrawHorizontalHighlightIndicator(false);
        lineDataSetIn.setDrawVerticalHighlightIndicator(false);

        lineDataSetIn.setColor(ContextCompat
                .getColor(this,R.color.color448DE4));

        lineDataSetIn.setLineWidth(2f);

        lineDataSetIn.setDrawCircles(false);

        lineDataSetIn.setDrawValues(false);

        lineDataSetIn.setMode(LineDataSet.Mode.LINEAR);


        List<ILineDataSet> dataSets = new ArrayList<>();

        dataSets.add(lineDataSetOut);
        dataSets.add(lineDataSetIn);

        LineData lineData = new LineData(dataSets);

        lcInOut.setData(lineData);


    }

    private void initInOutChartData(EntityInOut entityInOut){

        List<Entry> dataIn  = new ArrayList<>();

        for (int i = 0; i < entityInOut.getListIn().size(); i++) {

            dataIn.add(new Entry(i,entityInOut.getListIn().get(i).getValue()));

        }


        List<Entry> dataOut  = new ArrayList<>();

        for (int i = 0; i < entityInOut.getListOut().size(); i++) {

            dataOut.add(new Entry(i,entityInOut.getListOut().get(i).getValue()));

        }


        lcInOut.getXAxis().setValueFormatter(new ValueFormatter(){

            @Override
            public String getAxisLabel(float value, AxisBase axis) {


                return entityInOut.getListOut().get((int) value).getKey();
            }
        });


        LineData lineData = lcInOut.getLineData();

        LineDataSet lineDataSetOut = (LineDataSet) lineData.getDataSetByIndex(0);

        lineDataSetOut.setValues(dataOut);

        lineDataSetOut.notifyDataSetChanged();


        LineDataSet lineDataSetIn = (LineDataSet) lineData.getDataSetByIndex(1);

        lineDataSetIn.setValues(dataIn);

        lineDataSetIn.notifyDataSetChanged();

        lineData.notifyDataChanged();

        lcInOut.notifyDataSetChanged();

        lcInOut.invalidate();


    }



    private void initPriceChartData(List<EntityChartValue> values){

        List<Entry> data  = new ArrayList<>();

        for (int i = 0; i < values.size(); i++) {

            data.add(new Entry(i,values.get(i).getValue()));

        }

        lcprice.getXAxis().setValueFormatter(new ValueFormatter(){

            @Override
            public String getAxisLabel(float value, AxisBase axis) {


                return values.get((int) value).getKey();
            }
        });

        LineData lineData = lcprice.getLineData();

        LineDataSet lineDataSet = (LineDataSet) lineData.getDataSetByIndex(0);

        lineDataSet.setValues(data);

        lineDataSet.notifyDataSetChanged();

        lineData.notifyDataChanged();

        lcprice.notifyDataSetChanged();

        lcprice.invalidate();



    }
    // </editor-fold>
}
